package com.services;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.support.annotation.Nullable;

import com.bean.BookBean;
import com.ddd.wandemo.IOnNewBookListener;
import com.ddd.wandemo.ITestAidlInterface;
import com.util.LogUtil;

import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;

public class AidlTestService extends Service {
    private RemoteCallbackList<IOnNewBookListener> mListenerList = new RemoteCallbackList<>();
    private CopyOnWriteArrayList<BookBean> mBookList = new CopyOnWriteArrayList<>();
    private AtomicBoolean mIsServiceDestoryed = new AtomicBoolean(false);
    private Binder mBinder = new ITestAidlInterface.Stub() {
        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }

        @Override
        public void show(String msg) throws RemoteException {
            LogUtil.d(msg);
        }

        @Override
        public void addBook(BookBean book) throws RemoteException {
            LogUtil.d(book.getName()+"---"+book.getId());
        }

        @Override
        public String getBook(int id) throws RemoteException {
            return null;
        }

        @Override
        public void registerListener(IOnNewBookListener listener) throws RemoteException {
            mListenerList.register(listener);

            final int N = mListenerList.beginBroadcast();
            mListenerList.finishBroadcast();
            LogUtil.d("registerListener, current size:" + N);
        }

        @Override
        public void unregisterListener(IOnNewBookListener listener) throws RemoteException {
            boolean success = mListenerList.unregister(listener);

            if (success) {
                LogUtil.d( "unregister success.");
            } else {
                LogUtil.d( "not found, can not unregister.");
            }
            final int N = mListenerList.beginBroadcast();
            mListenerList.finishBroadcast();
            LogUtil.d("unregisterListener, current size:" + N);
        }
    };

    private void onNewBookArrived(BookBean book) throws RemoteException {
        mBookList.add(book);
        final int N = mListenerList.beginBroadcast();
        for (int i = 0; i < N; i++) {
            IOnNewBookListener l = mListenerList.getBroadcastItem(i);
            if (l != null) {
                try {
                    l.onNewBookArrived(book);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }
        mListenerList.finishBroadcast();
    }

    private class ServiceWorker implements Runnable {
        @Override
        public void run() {
            // do background processing here.....
            while (!mIsServiceDestoryed.get()) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int bookId = mBookList.size() + 1;
                BookBean newBook = new BookBean();
                newBook.setId(bookId);
                newBook.setName("aaaa--"+bookId);
                try {
                    onNewBookArrived(newBook);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mBookList.add(new BookBean("adsfsdas",2));
        mBookList.add(new BookBean( "Ios",2));
        new Thread(new ServiceWorker()).start();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

}
